package com.gage.controller.admin;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gage.controller.base.BaseController;
import com.gage.commons.result.PageInfo;
import com.gage.commons.utils.StringUtils;
import com.gage.model.BsAssociation;
import com.gage.model.BsDepartment;
import com.gage.model.BsDepartmentUser;
import com.gage.model.vo.UserVo;
import com.gage.service.IBsAssociationService;
import com.gage.service.IBsDepartmentService;

/**
 * <p>
 * 社团管理控制器
 * </p>
 * @author kairu.lu
 * @since 2017-04-26
 */
@Controller
@RequestMapping("/admin/association")
public class BsAssociationController extends BaseController{
	@Autowired
	private IBsAssociationService associationService;
	@Autowired
	private IBsDepartmentService departmentService;
	@GetMapping("/index")
    public String create() {
        return "admin/association/index";
    }
	 /**
		 * 加载树
		 * @author lkr
		 * @date:20170512
		 * @param request
		 * @param id
		 * @return
		 */
		@PostMapping("/tree")
	    @ResponseBody
		public Object getDepartmentTree(HttpServletRequest request,String id)
		{
			Integer pid=null;
			if (StringUtils.isEmpty(id))
			{
				pid = null;
			}else {
				pid=Integer.parseInt(id);
			}
			if (this.getUserType()==1) {
				pid=this.getDepartmentId();
			}
			return departmentService.selectDepartmentTree(null,pid);
		}
		/**
		 * 社团列表
	     * @author lkr
	     * @date:20170512
		 * @param association
		 * @param page
		 * @param rows
		 * @param sort
		 * @param order
		 * @return
		 */
	    @PostMapping("/dataGrid")
	    @ResponseBody
	    public Object dataGrid(BsAssociation association,String departmentid, Integer page, Integer rows, String sort, String order) {
	        PageInfo pageInfo = new PageInfo(page, rows, sort, order);
	        Map<String, Object> condition = new HashMap<String, Object>();
	        if (StringUtils.isNotEmpty(departmentid)) {
	            condition.put("departmentid", departmentid);
	        }
	        if (StringUtils.isNotEmpty(association.getAssociationName())) {
	            condition.put("associationName", association.getAssociationName());
	        }
	        pageInfo.setCondition(condition);
	          associationService.selectDataGrid(pageInfo);
	          return pageInfo;
	    }
	    /**
	     * 分配用户页
	     *
	     * @return
	     */
	    @GetMapping("/assignuser")
	    public String assignUser(Long id,Model model) {
	        return "admin/association/assignuser";
	    }
	    /**
	     * 分配用户页
	     *
	     * @return
	     */
	    @GetMapping("/userList")
	    public String userList(String id,Model model) {
	    	model.addAttribute("departmentId", id);
	        return "admin/association/userlist";
	    }
	    /**
	     * 用户列表
	     *
	     * @param userVo
	     * @param page
	     * @param rows
	     * @param sort
	     * @param order
	     * @return
	     */
	    @PostMapping("/assignUserList")
	    @ResponseBody
	    public Object assignUserList(UserVo userVo, Integer page, Integer rows, String sort, String order) {
	        PageInfo pageInfo = new PageInfo(page, rows, sort, order);
	        Map<String, Object> condition = new HashMap<String, Object>();

	        if (StringUtils.isNotBlank(userVo.getName())) {
	            condition.put("name", userVo.getName());
	        }
	        condition.put("organizationId",8);//默认获取大学部的用户
	        condition.put("roleId",10);//默认获取大学部管理员的用户
	        if (userVo.getCreatedateStart() != null) {
	            condition.put("startTime", userVo.getCreatedateStart());
	        }
	        if (userVo.getCreatedateEnd() != null) {
	            condition.put("endTime", userVo.getCreatedateEnd());
	        }
	        pageInfo.setCondition(condition);
	        return departmentService.selectAssignUserPage(pageInfo);
	    }
	    /**
	     * @author lkr
	     * @date:20170512
	     * @param article
	     * @param result
	     * @return
	     */
	    @PostMapping("/saveAssignUser")
	    @ResponseBody
		public Object saveAssignUser(@Valid BsDepartmentUser departmentUser,Long departmentIds, BindingResult result){
	    	if (result.hasErrors()) {
	            return renderError(result);
	        }
	    	int count=departmentService.getAssignUserCount(departmentIds);
	    	if (count>5) {
	    		return resultJson("1","系统最多只能分配5个管理账户！","");
			}
	    	departmentUser.setDepartmentId(departmentIds);
	    	departmentService.insertDepartmentUser(departmentUser);
	    	return resultJson("1","信息保存成功","");
	    }
	    /**
	     * 用户列表
	     *
	     * @param userVo
	     * @param page
	     * @param rows
	     * @param sort
	     * @param order
	     * @return
	     */
	    @PostMapping("/getDepartmentUserList")
	    @ResponseBody
	    public Object getDepartmentUserByDepartmentId(@Valid String departmentId, Integer page, Integer rows, String sort, String order) {
	        PageInfo pageInfo = new PageInfo(page, rows, sort, order);
	        Map<String, Object> condition = new HashMap<String, Object>();

	        if (StringUtils.isNotEmpty(departmentId)) {
	            condition.put("departmentId", departmentId);
	        }
	       // condition.put("organizationId",7);//默认获取大学部的用户
	        pageInfo.setCondition(condition);
	        return departmentService.getDepartmentUserByDepartmentId(pageInfo);
	    }
	    /**
	     * 删除
	     * @author lkr
	 	 * @date:20170512
	     * @param id
	     * @return
	     */
	    @PostMapping("/removeAssignUser")
	    @ResponseBody
	    public Object removeAssignUser(Long departmentId,Long userId) {
	    	if (StringUtils.isNotEmpty(departmentId)&&StringUtils.isNotEmpty(userId)) {
	    		departmentService.deleteByDepartmentIdAndUid(departmentId, userId);
	    		return renderSuccess("成功删除信息！");
	    	}else {
	    		return renderError("删除失败，删除操作出错！");
			}
		}
}
